package net.wendal.nutzbook;

import org.nutz.dao.Dao;
import org.nutz.dao.util.Daos;
import org.nutz.integration.quartz.NutQuartzCronJobFactory;
import org.nutz.ioc.Ioc;
import org.nutz.mvc.NutConfig;
import org.nutz.mvc.Setup;

import net.wendal.nutzbook.bean.User;
import net.wendal.nutzbook.service.UserService;

public class MainSetup implements Setup {

	public void init(NutConfig conf) {
		Ioc ioc = conf.getIoc();
		Dao dao = ioc.get(Dao.class);
		Daos.createTablesInPackage(dao, "net.wendal.nutzbook", false);
		
//		// 初始化默认根用户
//		if (dao.count(User.class) == 0) {
//			User user = new User();
//			user.setName("admin");
//			user.setPassword("123456");
//			user.setCreateTime(new Date());
//			user.setUpdateTime(new Date());
//			dao.insert(user);
//		}
        // 初始化默认根用户
        if (dao.count(User.class) == 0) {
            UserService us = ioc.get(UserService.class);
            us.add("admin", "123456");
        }
        // 测试发送邮件
//        try {
//            HtmlEmail email = ioc.get(HtmlEmail.class);
//            email.setSubject("测试NutzBook");
//            email.setMsg("This is a test mail ... :-)" + System.currentTimeMillis());
//            email.addTo("daihuan7@163.com");//请务必改成您自己的邮箱啊!!!
//            email.buildMimeMessage();
//            email.sendMimeMessage();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        // 获取NutQuartzCronJobFactory从而触发计划任务的初始化与启动
//      ioc.get(Scheduler.class);
	       ioc.get(NutQuartzCronJobFactory.class);
	       
	       //通过Daos.migration实现表结构自动修改
	       Daos.migration(dao, User.class, true, false, false);
	}
	
	public void destroy(NutConfig conf) {
	}

}
